/**
 * Created by Administrator on 2016/12/7.
 */


jGame.Network = {}

jGame.Network.get = function (url, json, callback, err_callback) {
    var xhr = cc.loader.getXMLHttpRequest();
    var paramsUrl = "";
    if(json) {
        for(var key in json){
            paramsUrl+="&"+key+"="+encodeURIComponent(json[key]);
        }
    }
    var getUrl = url;
    if(url.indexOf("?") > 0) {
        url += paramsUrl;
    }else {
        url += "?" + paramsUrl;
    }
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
            if(callback && typeof callback === "function") {
                var httpStatus = xhr.statusText;
                callback(xhr.responseText);
            }
        }else {
            if(err_callback && typeof err_callback === "function") {
                var httpStatus = xhr.statusText;
                err_callback(xhr.statusText);
            }
        }
    };
    xhr.send();
}

jGame.Network.post = function (url, json, callback, err_callback) {

    var xhr = cc.loader.getXMLHttpRequest();
    xhr.open("POST", url);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
            if(callback && typeof callback === "function") {
                var httpStatus = xhr.statusText;
                callback(xhr.responseText);
            }
        }else {
            if(err_callback && typeof err_callback === "function") {
                var httpStatus = xhr.statusText;
                err_callback(xhr.statusText);
            }
        }
    };
    xhr.send(JSON.stringify(json));
}